﻿using System.Collections.Generic;
using System.Linq;
using RSteel.DAO;
using Message = RSteel.DAL.Messages;

namespace RSteel.DAO
{
    public class MessagesDao : BaseDaoContext<Message>
    {
        protected MessagesDao()
        {
        }

        #region Singelton

        protected static MessagesDao instance;

        public static MessagesDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MessagesDao();
                }
                return instance;
            }
        }

        #endregion

        public void Add(string caption)
        {
            Message message = new Message
                                               {
                                                   Caption = caption
                                                   
                                               };
            Insert(message);
        }
        public List<Message> GetMessagesForAspRoles(string[] aspRoles)
        {
            using(var dc = GetDataContext())
            {
                List<Message> messageList = 
                    (
                    from aspnetRole in dc.AspnetRoles
                    where aspRoles.Contains(aspnetRole.RoleName.ToLower()) 
                    
                    from role in dc.Roles
                    where role.AspRoleId == aspnetRole.RoleId

                    from message in dc.Messages
                    where role.Id == message.RoleId
                    
                    orderby message.Order

                    select message).DefaultIfEmpty().ToList();

                return messageList;
            }
        }


    }
}